from dataclasses import asdict
from typing import List

from litemall.api.http import HttpRequests
from litemall.api.session_api import SessionApi
from litemall.api.cart import Cart


class CartApi:

    def __init__(self):
        self.token = SessionApi().login('user123', 'user123')
        self.http = HttpRequests(self.token)
        self.cart = Cart()

    def cart_add(self, cart: Cart):
        """添加购物车"""
        url = '/wx/cart/add'
        r = self.http.post(url, data=asdict(cart))
        return r

    def cart_index(self):
        """购物车列表"""
        url = '/wx/cart/index'
        r = self.http.get(url)
        return r

    def cart_delete(self, product_ids: List):
        """清空购物车"""
        url = '/wx/cart/delete'
        data = {"productIds": product_ids}
        r = self.http.post(url, data=data)
        return r
